<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="查询:">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="fetchData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="handleAdd(0)"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="list" stripe border style="width: 100%" row-key="id">
      <el-table-column align="center" label="序号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" prop="name" label="名称">
      </el-table-column>
      <el-table-column align="center" prop="url" label="请求地址">
      </el-table-column>
      <el-table-column align="center" prop="code" label="权限表示">
      </el-table-column>
      <el-table-column align="center" prop="type" label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1">目录</span>
          <span v-if="scope.row.type === 2">菜单</span>
          <span v-if="scope.row.type === 3">按钮</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="icon" label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序">
      </el-table-column>
      <el-table-column align="center" label="操作" width="255px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleAdd(scope.row.id)"
            >新增</el-button
          >
          <el-button
            type="success"
            size="mini"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <edit
      :title="edit.title"
      :visible="edit.visible"
      :formData="edit.formData"
      :remoteClose="remoteClose"
    ></edit>
  </div>
</template>

<script>
import api from "@/api/menu.js";
import Edit from "./edit.vue";
export default {
  data() {
    return {
      query: {},
      list: [],
      edit: {
        title: "",
        visible: false,
        formData: {},
      },
    };
  },

  components: { Edit },
  created() {
    this.fetchData();
  },

  methods: {
    fetchData() {
      api.getList(this.query).then((res) => {
        this.list = res.data;
      });
    },
    handleAdd(id) {
      //如果id=0，则是在条件查询处点击的是一级菜单，否则是某个菜单下的二级菜单
      this.edit.formData.parentId = id;
      this.edit.title = "新增";
      this.edit.visible = true;
    },
    handleEdit(id) {
      api.getById(id).then((res) => {
        if (res.code === 20000) {
          this.edit.visible = true;
          this.edit.title = "编辑";
          this.edit.formData = res.data;
        }
      });
    },
    handleDelete(id) {
      this.$confirm("确认删除这条记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.delete(id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
          });
          this.fetchData();
        })
        .catch(() => {});
    },
    //重置
    reload() {
      this.query = {};
      this.fetchData();
    },
    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },
  },
};
</script>

<style scoped>
</style>